﻿using System;
using System.Collections.Generic;

using Gonte;
using Gonte.ObjectModel;
using Gonte.Validation;

using Microsoft.VisualStudio.TestTools.UnitTesting;


namespace GonteUtilsTest
{
    public class TestValidationObject
    {
        private int rating_;

        [RangeField(1, 10)]
        public int Rating
        {
            get { return rating_; }
            set { rating_ = value; }
        }

        private String email_;

        [RegexField(RegexPattern.Email)]
        public String Email
        {
            get { return email_; }
            set { email_ = value; }
        }

        private String name_;

        [NullableField(false)]
        public String Name
        {
            get { return name_; }
            set { name_ = value; }
        }

        private String description_;

        [RangeField("aaaaa", "ccccc")]
        [LengthField(1, 4)]
        public String Description
        {
            get { return description_; }
            set { description_ = value; }
        }

        private DateTime date_;

        [RangeField("1/1/2000", "1/1/2005")]
        public DateTime Date
        {
            get { return date_; }
            set { date_ = value; }
        }
    }

    public class ValidableObject
        : IValidable
    {
        #region IValidable Members

        public bool Validate(ref List<ValidationError> errors)
        {
            errors = new List<ValidationError>();
            errors.Add(new ValidationError("MyProperty", "My error"));
            return false;
        }

        #endregion
    }

    /// <summary>
    ///This is a test class for Gonte.Validation.ObjectValidator and is intended
    ///to contain all Gonte.Validation.ObjectValidator Unit Tests
    ///</summary>
    [TestClass()]
    public class ObjectValidatorTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }
        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for Validate (object, out List&lt;ValidationError&gt;)
        ///</summary>
        [TestMethod()]
        public void ValidateObjectTest()
        {
            TestValidationObject obj = new TestValidationObject();
            obj.Rating = 11;
            obj.Email = "jgonte@gmail,com";
            obj.Description = "bbbbb";

            List<ValidationError> errors = new List<ValidationError>();
            bool valid = ObjectValidator.Validate(obj, ref errors);

            foreach (ValidationError error in errors)
            {
                Console.WriteLine(error.ToString());
            }

            Assert.AreNotEqual(valid, true);
        }

        [TestMethod()]
        public void ValidateObjectWithIValidableTest()
        {
            ValidableObject obj = new ValidableObject();

            List<ValidationError> errors = new List<ValidationError>();
            bool valid = ObjectValidator.Validate(obj, ref errors);

            Assert.AreNotEqual(valid, true);
            Assert.AreEqual("Validation failed for property: MyProperty - My error", errors[0].ToString());
        }
    }
}
